﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"
#include "findpassword.h"

#include <QDebug>
#include <QByteArray>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow),
      file("user.txt")
{
    ui->setupUi(this);

    if(!file.open(QIODeviceBase::ReadWrite | QIODeviceBase::Text))
    {
        file.open(QIODeviceBase::NewOnly | QIODeviceBase::ReadWrite | QIODeviceBase::Text);
    }
    tobehandle = false;
    signupname.clear();
    signpasswd.clear();
    signupquestion.clear();

//    finduserinfo();
}

MainWindow::~MainWindow()
{
    file.close();
    delete ui;
}

int MainWindow::customDialog(const QStringList &allbtn, const QString &text, const QString &title)
{
    QMessageBox *cusmess = new QMessageBox(this);

    cusmess->setWindowTitle(title);
    cusmess->setText(text);

    for(int i = 0; i < allbtn.size(); i++)
    {
        cusmess->addButton(allbtn.at(i), QMessageBox::YesRole);
    }

    return cusmess->exec();
}



void MainWindow::on_pushButton_sign_in_clicked()
{
    QString username = ui->lineEdit_username->text();
    QString passwd = ui->lineEdit_password->text();

    qDebug() << username;
    qDebug() << passwd;

    bool isExit = false;
    bool usernametrue = false;

    file.seek(0);

    while (true)
    {
        QByteArray line = file.readLine();
        if(line.isEmpty())
            break;
        if(line.contains(QString("username=%1").arg(username).toStdString().c_str()))
        {
            usernametrue = true;
            continue;
        }
        if(usernametrue)
        {
            if(line.contains(QString("passwd=%1").arg(passwd).toStdString().c_str()))
                isExit = true;

            break;
        }
        usernametrue = false;
    }

    if(!isExit)
    {
        QStringList listbtn;
        listbtn << "忽略" << "注册";
        int ret = customDialog(listbtn, "您输入的账号不存在");
        if(ret == 1)
        {
            Dialog *signup = new Dialog(this);
            signup->exec();
            qDebug() << this;
        }
    }
    else
    {
        QStringList list;
        list << "确定";
        customDialog(list, "登陆成功", "提示");
    }
}

void MainWindow::setTobehandle(bool newTobehandle)
{
    tobehandle = newTobehandle;
}

bool MainWindow::filecontains(const QString &arg)
{

    file.seek(0);

    while (true)
    {
        QByteArray line = file.readLine();
        if(line.isEmpty())
            break;
        if(line.contains(arg.toStdString().c_str()))
        {
            return true;
        }
    }

    return false;
}



const QFile &MainWindow::getFile() const
{
    return file;
}

void MainWindow::write()
{
    if(!tobehandle)
        return;
    if(!file.isOpen())
        return;

    file.seek(0);
    file.seek(file.size());



    file.write("{\n");
    file.write(QString("username=%1").arg(signupname).toUtf8());
    file.write("\n");
    file.write(QString("passwd=%1").arg(signpasswd).toUtf8());
    file.write("\n");
    file.write(QString("question=%1").arg(signupquestion).toUtf8());
    file.write("\n");
    file.write("}\n");

    file.flush();
}

void MainWindow::setSignupquestion(const QString &newSignupquestion)
{
    signupquestion = newSignupquestion;
}

void MainWindow::setSignupname(const QString &newSignupname)
{
    signupname = newSignupname;
}

void MainWindow::setSignpasswd(const QString &newSignpasswd)
{
    signpasswd = newSignpasswd;
}


void MainWindow::on_pushButton_sign_up_clicked()
{

    Dialog *signup = new Dialog(this);
    signup->setAttribute(Qt::WA_DeleteOnClose);
    signup->exec();
}


void MainWindow::on_pushButton_findpasswd_clicked()
{
    findpassword *findpwdialog  = new findpassword(this);
    findpwdialog->setUsernametext(ui->lineEdit_username->text());

    findpwdialog->setAttribute(Qt::WA_DeleteOnClose);
    findpwdialog->exec();

}

